<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\common\library\junziqian;

use app\common\library\helper;
use cores\traits\ErrorTrait;

/**
 * 模块驱动
 * Class Driver
 * @package app\common\library\sms
 */
class Driver extends Config
{
    use ErrorTrait;

    // 后台短信配置信息
    private $smsConfig;

    /**
     * 企业认证
     */
    public function organizationCreate(array $data)
    {
        $sign = $this->getSign();
        $url  = $this->url . '/v2/user/organizationCreate';

        $result = $this->post($url, $data, $sign);

        // 记录日志
        helper::logInfo('君子签合同下载路径', [
            'sign'       => $sign,
            'data'       => $data,
            'result'     => $result,
            'sendErrMsg' => $this->getError(),
        ]);
        // 存在异常时抛错
        // $sendStatus === false && throwError($this->getError());
        return $result;
    }

    /**
     * 更新个人证书
     *
     */
    public function cloudCertiPerInfo($data)
    {
        $sign = $this->getSign();
        $url  = $this->url . '/v2/user/cloudCertiPerInfo';

        $result = $this->post($url, $data, $sign);

        // 记录日志
        helper::logInfo('君子签更新个人证书', [
            'sign'       => $sign,
            'data'       => $data,
            'result'     => $result,
            'sendErrMsg' => $this->getError(),
        ]);
        // 存在异常时抛错
        $result['success'] === false && throwError($result['msg']);
        return $result;
    }

    /**
     * 身份证验证
     *
     */
    public function getIdcardOcr($imgHttpUrl, $name)
    {
        $sign               = $this->getSign();
        $url                = $this->url . '/v2/ocr/ocrIdentity';
        $data['imgHttpUrl'] = $imgHttpUrl;
        $data['front']      = $name == 'idcard1' ? 1 : 0;

        $result = $this->post($url, $data, $sign);

        // 记录日志
        helper::logInfo('君子签身份证验证', [
            'sign'       => $sign,
            'data'       => $data,
            'result'     => $result,
            'sendErrMsg' => $this->getError(),
        ]);
        // 存在异常时抛错
        $result['success'] === false && throwError($result['msg']);
        return $result;
    }
    /**
     * 获取签约状态
     */
    public function getContractStatus(array $data)
    {
        $sign    = $this->getSign();
      
        $url     = $this->url . '/v2/sign/status ';

        $result = $this->post($url, $data, $sign);

        // 记录日志
        helper::logInfo('君子签签约状态', [
            'sign'       => $sign,
            'data'       => $data,
            'result'     => $result,
            'sendErrMsg' => $this->getError(),
        ]);
        // 存在异常时抛错
        $result['success'] === false && throwError($result['msg']);
        return $result;
    }
    /**
     * 获取签约链接
     */
    public function getLink(array $data)
    {
        $sign    = $this->getSign();
      
        $url     = $this->url . '/v2/sign/link';

        $result = $this->post($url, $data, $sign);

        // 记录日志
        helper::logInfo('君子签签约链接', [
            'sign'       => $sign,
            'data'       => $data,
            'result'     => $result,
            'sendErrMsg' => $this->getError(),
        ]);
        // 存在异常时抛错
        $result['success'] === false && throwError($result['msg']);
        return $result;
    }

    /**
     * 获取合同
     */
    public function getLinkFile(array $data)
    {
        $sign = $this->getSign();
        $url  = $this->url . '/v2/sign/linkFile';

        $result = $this->post($url, $data, $sign);

        // 记录日志
        helper::logInfo('君子签合同下载路径', [
            'sign'       => $sign,
            'data'       => $data,
            'result'     => $result,
            'sendErrMsg' => $this->getError(),
        ]);
        // 存在异常时抛错
        $result['success'] === false && throwError($result['msg']);
        return $result;
    }

    /**
     * 签约
     */
    public function applySign(array $data)
    {
        $sign = $this->getSign();
        $url  = $this->url . '/v2/sign/applySign';

        $result = $this->post($url, $data, $sign);

        // 记录日志
        helper::logInfo('君子签签约', [
            'sign'       => $sign,
            'data'       => $data,
            'result'     => $result,
            'sendErrMsg' => $this->getError(),
        ]);
        // 存在异常时抛错
        // $sendStatus === false && throwError($this->getError());
        return $result;
    }

    /**
     * 获取人脸结果
     */
    public function getFaceResult(array $data)
    {
        $sign = $this->getSign();
        $url  = $this->url . '/v2/auth/startH5Face';

        $result = $this->post($url, $data, $sign);

        // 记录日志
        helper::logInfo('君子签人脸url', [
            'sign'       => $sign,
            'data'       => $data,
            'result'     => $result,
            'sendErrMsg' => $this->getError(),
        ]);
        // 存在异常时抛错
        // $sendStatus === false && throwError($this->getError());
        return $result;
    }

    /**
     * 小程序人脸认证
     */
    public function startMiniAppFace(array $data)
    {
        $sign = $this->getSign();
        $url  = $this->url . '/v2/auth/startMiniAppFace';

        $result = $this->post($url, $data, $sign);

        // 记录日志
        helper::logInfo('君子小程序人脸认证', [
            'sign'   => $sign,
            'data'   => $data,
            'result' => $result,
        ]);
        // 存在异常时抛错
        $result['success'] === false && throwError($result['msg']);
        return $result;
    }

    /**
     * 获取人脸H5链接
     */
    public function getFaceUrl(array $data)
    {
        $sign = $this->getSign();
        $url  = $this->url . '/v2/auth/startH5Face';

        $result = $this->post($url, $data, $sign);

        // 记录日志
        helper::logInfo('君子签人脸url', [
            'sign'       => $sign,
            'data'       => $data,
            'result'     => $result,
            'sendErrMsg' => $this->getError(),
        ]);
        // 存在异常时抛错
        // $sendStatus === false && throwError($this->getError());
        return $result;
    }

    /**
     * 模拟POST请求 [第二种方式, 用于兼容微信api]
     * @param $url
     * @param array $data
     * @return mixed
     * @throws \cores\exception\BaseException
     */

    protected function post($url, array $data = [], $sign)
    {

        $data = array_merge($data, $sign);

        $curl = curl_init();

        curl_setopt_array($curl, [
            CURLOPT_URL            => $url,
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_CUSTOMREQUEST  => 'POST',

            CURLOPT_POSTFIELDS     => http_build_query($data),

            CURLOPT_HTTPHEADER     => [
                'Content-Type: application/x-www-form-urlencoded',
                'Accept: application/json',
            ],

        ]);
        $result = curl_exec($curl);
        curl_close($curl);
        $response = helper::jsonDecode($result);
        return $response;
    }

    /**
     * 获取签名
     * @param array $result
     * @return bool
     */
    private function getSign()
    {
        /**默认加密方式:不输入使用sha256,其它可选择项md5,sha1,sha3-256*/
        $ts = time();
        if ($this->tsType == 1) {
            $ts = $ts * 1000;
        }

        $sign    = null;
        $nonce   = md5($ts . "");
        $signSrc = "nonce" . $nonce . "ts" . $ts . "app_key" . $this->appkey . "app_secret" . $this->appSecret;

        if ($this->encryMethod == null || $this->encryMethod == "sha256") {
            $sign = ShaUtils::getSha256($signSrc);
        } else if ($this->encryMethod == "sha1") {
            $sign = ShaUtils::getSha1($signSrc);
        } else if ($this->encryMethod == "md5") {
            $sign = md5($signSrc);
        } else {
            throw new Exception($this->encryMethod . ",必须为md5,sha1,sha256之一", "PARAM_ERROR");
        }

        $req['ts']      = $ts;
        $req['app_key'] = $this->appkey;
        $req['sign']    = $sign;
        $req['nonce']   = $nonce; //这只是为了生成一个随机值
        if ($this->encryMethod != null) {
            $req['encry_method'] = $this->encryMethod; //为''也不能传
        }
        return $req;
    }

}
